/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iscte.iul.dcti.pa.gspas.gui;

import java.util.List;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import pt.iscte.iul.dcti.pa.gspas.gui.listener.CollectionMouseListener;

import pt.iscte.iul.dcti.pa.gspas.model.Course;
import pt.iscte.iul.dcti.pa.gspas.util.Property;
import pt.iscte.iul.dcti.pa.gspas.util.SwingUtils;

/**
 *
 * @author Gedsimon Pereira - 58554
 * @contact gspas@iscte.pt
 *
 */
public class AssignmentWindow extends JFrame {

    private JPanel panelRight;

    private final Object object;
    /**
     *
     */
    private static final long serialVersionUID = 5879894030485928482L;

    public AssignmentWindow(Object student) {
        this.object = student;
        initComponents();
    }

    private void initComponents() {
        // Common configuration
        Class<? extends Object> objectClass = object.getClass();
        String name = objectClass.getSimpleName();

        //setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Add " + name);

        setLocationRelativeTo(null);
        setSize(350, 200);

        JSplitPane basePanel = new JSplitPane();
        basePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(name
                + " Information"));

        List<Property> properties = Property.getProperties(objectClass);

        panelRight = new JPanel();
        panelRight.add(new JLabel(""));
        JButton btnDone = new JButton();
        btnDone.setText("Done");
        panelRight.add(btnDone);

        JPanel panelLeft = new JPanel();
        int lineNumber = 0;

        for (Property property : properties) {
            switch (property.getType()) {
                case BOOLEAN:
                    panelLeft.add(SwingUtils.createLabel(property.getLabel()));
                    panelLeft.add(SwingUtils.createBoolean("", property.getValue(object, Boolean.class)));
                    lineNumber++;
                    break;
                case INT:
                    panelLeft.add(SwingUtils.createLabel(property.getLabel()));
                    panelLeft.add(SwingUtils.createTxtField(property.getValue(object, int.class).toString()));
                    lineNumber++;
                    break;
                case DOUBLE:
                    panelLeft.add(SwingUtils.createLabel(property.getLabel()));
                    panelLeft.add(SwingUtils.createTxtField(property.getValue(object, int.class).toString()));
                    lineNumber++;
                    break;
                case ENUM:

                    break;
                case STRING:
                    panelLeft.add(SwingUtils.createLabel(property.getLabel()));
                    panelLeft.add(SwingUtils.createTxtField(property.getValue(object, String.class)));
                    lineNumber++;
                    break;
                case COLLECTION:
                    JLabel label;
                    panelLeft.add(label = SwingUtils.createLabel(property.getLabel()));
                    label.addMouseListener(new CollectionMouseListener(object,
                            property, panelRight));
                    panelLeft.add(SwingUtils.createLabel(""));
                    lineNumber++;
                    break;
                default:
                    break;
            }
        }
        panelLeft.setLayout(new java.awt.GridLayout(lineNumber, 2));

        basePanel.setLeftComponent(panelLeft);
        basePanel.setRightComponent(panelRight);
        this.add(basePanel);

        this.revalidate();
    }

    public static void main(String[] args) {
        try {
            javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels = javax.swing.UIManager
                    .getInstalledLookAndFeels();
            for (int idx = 0; idx < installedLookAndFeels.length; idx++) {
                if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
                    javax.swing.UIManager
                            .setLookAndFeel(installedLookAndFeels[idx]
                                    .getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AssignmentWindow.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AssignmentWindow.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AssignmentWindow.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AssignmentWindow.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        Course course = new Course();
        course.setName("PA 2013");
        course.addAssignment("A");
        course.addStudent(1, "Ged");
        course.addStudent(2, "Alv");
        new AssignmentWindow(course).setVisible(true);

    }

}
